/**
 * Copyright (c) Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package ronp.anim.gui.tools;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ronp.anim.data.filmstrip.Animation;
import ronp.anim.data.objects.RNObject;
import ronp.anim.gui.SelectedState;
import ronp.anim.gui.viewer.AnimViewer;

/**
 * Represents that tab of {@link RNObject}s
 * 
 * @author Daniel Centore
 *
 */
public class ObjectsMenu extends JPanel
{
	private static final long serialVersionUID = 1L;

	private final JList list; // The JList

	public ObjectsMenu(final AnimViewer viewer, final Animation anim, final SelectedState state)
	{
		this.setLayout(new BorderLayout());

		list = new JList(anim.getObjects());
		list.addMouseListener(new MouseListener()
		{
			@Override
			public void mouseReleased(MouseEvent e)
			{
				if (e.getButton() == 3)
				{
					JPopupMenu popup = new JPopupMenu();
					JMenuItem menuItem = new JMenuItem("Delete");
					menuItem.addActionListener(new ActionListener()
					{
						@Override
						public void actionPerformed(ActionEvent e)
						{
							for (Object o : list.getSelectedValues())
							{
								anim.removeObject((RNObject) o);
							}
						}
					});
					popup.add(menuItem);

					popup.show(list, e.getX(), e.getY());
				}
			}

			@Override
			public void mousePressed(MouseEvent e)
			{
				// ignore
			}

			@Override
			public void mouseExited(MouseEvent e)
			{
				// ignore
			}

			@Override
			public void mouseEntered(MouseEvent e)
			{
				// ignore
			}

			@Override
			public void mouseClicked(MouseEvent e)
			{
				//ignore
			}
		});

		list.getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{
			@Override
			public void valueChanged(ListSelectionEvent e)
			{
				viewer.repaint();
			}
		});

		this.add(list);

		viewer.setMenu(this);
	}

	/**
	 * Checks if an {@link RNObject} is currently selected
	 * @param object Object to check
	 * @return True if it is selected (might not be the only one!); False otherwise
	 */
	public boolean isSelected(RNObject object)
	{
		for (Object o : list.getSelectedValues())
		{
			if (o == object)
				return true;
		}

		return false;
	}

	public JList getList()
	{
		return list;
	}
}
